//
//  WCProjectBuildStatusTextFieldCell.m
//  WabbitCode
//
//  Created by William Towe on 2/11/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCProjectBuildStatusTextFieldCell.h"


static const CGFloat kImageAndStringPadding = 3.0;
static const CGFloat kImageAndStringPairPadding = 4.0;

@implementation WCProjectBuildStatusTextFieldCell
- (void)dealloc {
	[_imagesAndStrings release];
	[super dealloc];
}

@synthesize imagesAndStrings=_imagesAndStrings;

- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view {
	if (![self imagesAndStrings]) {
		[self setStringValue:@""];
		return;
	}
	
	static NSDictionary *stringAttrs = nil;
	if (!stringAttrs) {
		NSMutableParagraphStyle *style = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
		[style setAlignment:NSRightTextAlignment];
		
		stringAttrs = [[NSDictionary alloc] initWithObjectsAndKeys:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:[self controlSize]]], NSFontAttributeName, style, NSParagraphStyleAttributeName, nil];
	}
	
	NSArray *objects = [self imagesAndStrings];
	
#ifdef WCDEBUG
	NSAssert([objects count] % 2 == 0, @"WCProjectBuildStatusTextFieldCell given unequal number of images and strings");
#endif
	
	for (NSInteger index = [objects count]-1; index >= 0; index-=2) {
		NSString *string = [objects objectAtIndex:index];
		NSImage *image = [objects objectAtIndex:index-1];
		
		NSAttributedString *at = [[[NSAttributedString alloc] initWithString:string attributes:stringAttrs] autorelease];
		
		NSRect bounds = [at boundingRectWithSize:frame.size options:NSStringDrawingOneShot];
		[at drawInRect:frame];
		
		frame.size.width -= bounds.size.width+kImageAndStringPadding;
		
		NSSize size = [image size];
		
		[image compositeToPoint:NSMakePoint(frame.origin.x+frame.size.width-size.width, frame.origin.y+floor((frame.size.height+size.height)/2)) operation:NSCompositeSourceOver];
		
		frame.size.width -= size.width+kImageAndStringPairPadding;
	}
}
@end
